<?php

namespace App\Http\Controllers\Admin;

use App\Repositories\ModuleRepository;
use App\Repositories\PermissionRepository;
use App\Utils\ViewHelper;
use Illuminate\Http\Request;
use App\Http\Requests\PermissionSaveRequest;

/**
 * 控制器
 * Class PermissionController
 * @package App\Http\Controllers\Admin
 */
class PermissionController extends BaseController
{
    private $rep;

    public function __construct(PermissionRepository $rep)
    {
        $this->set('permission');
        $this->rep = $rep;
    }

    function index($module_id,Request $request)
    {
        $search = $request->all();
        $search['module_id'] = $module_id;
        $page = $this->rep->adminPager($search);
        return $this->view('index',compact('page','module_id'));
    }

    function create($module_id,ModuleRepository $repository)
    {
        $result = $repository->editInfo($module_id);
        if ($result->isError()) {
            ViewHelper::E404($result->msg);
        }
        $controllerName=$result->data->tag;
        $actions = $repository->getNotAddAction($controllerName);
        return $this->view('create', compact('actions', 'module_id','controllerName'));
    }

    function save(PermissionSaveRequest $request)
    {
        $data = $request->all();
        $id = $request->get('id', 0);
        return $this->rep->save($data, $id);
    }

    function edit($id)
    {
        $result = $this->rep->editInfo($id);
        if ($result->isSuccess()) {
            return $this->view('edit')->with('entity', $result->data);
        }
        return ViewHelper::E404($result->msg);
    }

    function destroy($id)
    {
        return $this->rep->destroy($id);
    }
}
